package com.silence.reflection;


import com.sun.xml.internal.ws.api.server.SDDocument;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class reflection {
    public static void main(String[] args) throws Exception {
        // 获取类
        Class c1 = Student_reflection.class;
//        System.out.println(c1.getName()); // 全类名
//        System.out.println(c1.getSimpleName()); // 类名

        Class c2 = Class.forName("com.silence.reflection.Student_reflection");

//        Student_reflection s1 = new Student_reflection();
//        Class c3 = s1.getClass();

        // 获取类的所有构造器
        Constructor[] con1 = c1.getDeclaredConstructors();
//        for (Constructor constructor : con1) {
//            System.out.println(constructor.getParameterCount());
//        }
        // 获取类的单个构造器
        Constructor con1single = c1.getDeclaredConstructor(String.class, int.class, int.class, String.class);
        Constructor con2single = c1.getDeclaredConstructor();

        con2single.setAccessible(true);
        Student_reflection s1 = (Student_reflection) con2single.newInstance();
//        System.out.println(s1);
        Student_reflection s2 = (Student_reflection) con1single.newInstance("xm", 18, 1, "hello");
//        System.out.println(s2);

        // 成员变量
        Field[] fields = c1.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName() + " " + field.getType());
        }
        Field field_name = c1.getDeclaredField("name");
        Field field_age = c1.getDeclaredField("age");
        field_name.setAccessible(true);
        field_name.set(s2,"小明");
        field_age.setAccessible(true);
        field_age.set(s2,24);
        System.out.println(s2);
        // 成员方法
        Method[] methods = c1.getDeclaredMethods();

        Method method_setName = c1.getDeclaredMethod("setName", String.class);
        System.out.println(method_setName);
        System.out.println(method_setName.getParameterCount());
        System.out.println(method_setName.getReturnType());
        Object result = method_setName.invoke(s2,"小军");
        System.out.println(s2+" "+ result);
        ObjectSaveReflection.saveObject(s2);
    }
}
